﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using System.Threading;
using System.Threading.Tasks;
using COFE.Core.Filter;

namespace COFE.Core
{
    public class CustomEntryList : EntryListBase, ICustomEntryList
    {
        #region Constructor

        public CustomEntryList(Guid id, string name)
            : base(id, name)
        {
            base.Status = StatusType.Completed;
        }

        public CustomEntryList(string name)
            : this(Guid.NewGuid(), name)
        {

        }

        #endregion

        #region Methods


        public void InsertRange(int index, IEnumerable<IFileSystemInfo> entries)
        {
            var entryList = new List<IFileSystemInfo>(entries);

            lock (_entryList)
            {
                for (int i = entryList.Count - 1; i >= 0; i--)
                    if (_entryList.Contains(entryList[i]))
                        _entryList.RemoveAt(i);

                if (index == -1)
                    _entryList.AddRange(entryList);
                else _entryList.InsertRange(Math.Min(entryList.Count, index), entryList);
            }
            //while (etor.MoveNext())
            //    if (!_entryList.Contains(etor.Current))
            //        _entryList.Add(etor.Current);            
        }

        public void Delete(IEntryFilter[] matchFilters)
        {
            lock (_entryList)
                for (int i = _entryList.Count - 1; i >= 0; i++)
                    if (matchFilters.MatchAll(_entryList[i] as IInternalFileSystemInfoExA,
                        MatchType.MatchThisEntry))
                        _entryList.RemoveAt(i);
        }

        #endregion

        #region Data


        private List<IFileSystemInfo> _entryList = new List<IFileSystemInfo>();

        #endregion

        #region Public Properties

        public override IEnumerable<IFileSystemInfo> Entries { get { return _entryList; } }

        #endregion



    }
}
